config = new Config({
    server : "http://localhost/proyek/SIRA/apps/server/",
    location : {
        id : "01",
        name : "Salman"
    }
});
fk = new Fk({
    load : function(){
        $.ajax({
            url: config.server+"fk.json",
            cache: false,
            dataType: "json",
            success: function(data){
                fk.data = data;
                var Set = function(each,index,text,value,parent,keyText,keyValue,keyParent){
                    var first = new Object();
                        first[keyText] = "-";
                        first[keyValue] = "null";
                        if (parent){
                            first[keyParent] = "null";
                        };
                    console.log(first);
                    fk[index] = new kendo.data.DataSource({
                        data : [first]
                    });
                    fk[index].read();
                    $.each(each,function(i,n){
                        var isi = new Object();
                        isi[keyText] = n[text];
                        isi[keyValue] = n[value];
                        if (parent){
                            isi[keyParent] = n[parent];
                        };
                        fk[index].add(isi);
                    });
                };
                Set(data.agama,"agama","nama_agama","id_agama",null,"namaAgama","idAgama",null);
                Set(data.goldarah,"darah","nama_goldarah","id_goldarah",null,"namaGoldarah","idGoldarah");
                Set(data.kelamin,"kelamin","nama_kelamin","id_kelamin",null,"namaKelamin","idKelamin",null);
                Set(data.pendidikan,"pendidikan","nama_pendidikan","id_pendidikan",null,"namaPendidikan","idPendidikan",null);
                Set(data.alamat_kategori,"alamat_kategori","nama_kategori","id_kategori",null,"namaKategori","idKategori",null);
                Set(data.alamat_tipe,"alamat_tipe","tipe","id_tipe",null,"namaTipe","idTipe",null);
                Set(data.komunikasi_jenis,"komunikasi_jenis","nama_jenis","id_jenis",null,"namaJenis","idJenis",null);
                Set(data.propinsi,"provinsi","nama_propinsi","id_propinsi",null,"namaProvinsi","idProvinsi",null);
                Set(data.kota,"kota","nama_kota","id_kota","propinsi_id","namaKota","idKota","idProvinsi");
                Set(data.kecamatan,"kecamatan","nama_kecamatan","id_kecamatan","kota_id","namaKecamatan","idKecamatan","idKota");
                Set(data.kelurahan_desa,"kelurahan","nama_kelurahan_desa","id_kelurahan_desa","kecamatan_id","namaKelurahan","idKelurahan","idKecamatan");
                //Set(data.user_level,"user_level","nama_level","id_level",null);
                Set(data.srakategori,"donatur_kategori","nama_kategori","id_kategori",null,"namaKategori","idKategori",null);
                Set(data.srapekerjaan,"pekerjaan","nama_pekerjaan","id_pekerjaan",null,"namaPekerjaan","idPekerjaan",null);
                Set(data.sramedia,"media","nama_media","id_media",null,"namaMedia","idMedia",null);
                Set(data.sratransaksi_tipe,"transaksi_tipe","nama_tipe","id_tipe",null,"namaTipe","idTipe",null);
                Set(data.sratransaksi_tipe_det,"transaksi_tipe_det","tipe_det_nama","id_tipe_det","tipe_id","namaTipeDet","idTipeDet","idTipe");
                Set(data.sratransaksi_via,"transaksi_via","nama_via","id_via",null,"namaVia","idVia",null);
                Set(data.sratransaksi_via_det,"transaksi_via_det","via_det_nama","id_cia_det","via_id","namaViaDet","idViaDet","idVia");
                Set(data.srarecord_action,"record_action","nama_action","id_action",null,"namaAction","idAction","null");
                //Set(data.Data[14].hrs_master_jabatan,"jabatan","nama_jabatan","id_jabatan",null);
                //Create dropdown FK
                Set(data.pendidikan,"pendidikanDropdown","nama_pendidikan","id_pendidikan",null,"text","value",null);
                //Load model
                model = {
                    login : kendo.observable({
                        username : null,
                        password: null
                    }),
                    user : kendo.observable({
                        user : null,
                        nama : null,
                        jabatan : null,
                        token : null,
                    }),
                    donatur : kendo.observable({
                        idDonatur : null,
                        idPerson : null,
                        tanggalDaftar : config.getDate(),
                        personKategori : 3101,
                        agama : 1,
                        nama : null,
                        publikasi : null,
                        email : null,
                        darahs : fk.darah.view(),
                        darah : null,
                        kelamins : fk.kelamin.view(),
                        kelamin : null,
                        tempatLahir : null,
                        tanggalLahir : null,
                        pendidikans : fk.pendidikan.view(),
                        pendidikan : "null",
                        kategoris : fk.donatur_kategori.view(),
                        kategori : null,
                        kategoriKet : null,
                        pekerjaans : fk.pekerjaan.view(),
                        pekerjaan : null,
                        pekerjaanKet : null,
                        medias : fk.media.view(),
                        media : null,
                        mediaKet : null,
                        catatan : null
                    }),
                    transaksi : kendo.observable({
                        tanggalInput : config.getDate(),
                        tanggalTransaksi : null,
                        kuitansi : null,
                        donatur : null,
                        tipes : fk.transaksi_tipe.view(),
                        tipe : null,
                        tipeDets : fk.transaksi_tipe_det.view(),
                        tipeDet : null,
                        vias : fk.transaksi_via.view(),
                        via : null,
                        viaDets : fk.transaksi_via_det.view(),
                        viaDet : null,
                        jumlah : null,
                        catatan : null,
                        penerima : null,
                        pencatat : null
                    })
                };
                //Load some dependencies
                db.donatur.source.read();
                formz.addDonatur.inject();
                formz.addDonatur.load();
                formz.addTransaksi.inject();
                formz.addTransaksi.load();
                frame.startUp.hide();
                formz.login.show();
                //Load Grid
                grid = {
                    donatur : new Grid({
                        dataSource : db.donatur.source,
                        columns : [
                            { field: "id_person", title: "ID", width:"110px" },
                            { field: "nama", title: "Nama" },
                            { field: "email", title: "E-Mail" },
                            { field: "darah", title: "Gol. Darah" },
                            { field: "kelamin", title: "Jenis Kelamin" },
                            { field: "pendidikan", title: "Pendidikan", values : fk.pendidikanDropdown.view() },
                            { field: "pekerjaan", title: "Pekerjaan" },
                            { field: "tempat_lahir", title: "Kota Lahir" },
                            { field: "tanggal_lahir", title: "Tangal Lahir" },
                            { field: "kategori", title: "Kategori" },
                            { command: ["edit", "destroy"], width : "200px" }
                        ],
                        placeholder : "#donatur"
                    })
                };
                grid.donatur.inject();
            }
        });
    }
});
db = {
    donatur : new DbModel({
        url : config.server,
        dataC : {
            menu : 31,
            submenu : 319
        },
        dataR : {
            act : "viewDonatur"
        },
        dataU : {
            menu : 31,
            submenu : 339
        },
        dataD : {
            menu : 31,
            submenu : 349
        },
        schema : {
            model : {
                id : "id_donatur",
                fields : {
                    id_donatur : {
                        editable : false
                    },
                    pendidikan : {
                        field : "pendidikan",
                        type : "number",
                        defaultValue : 1
                    }
                }
            }
        },
        getNewId : function () {
                var last = this.source.data().length;
                var lastId = this.source.data()[last-1].id;
                var id = function(){
                        var x = lastId[6]+lastId[7]+lastId[8]+lastId[9]+lastId[10];
                        var y = Number(x)+1;
                        if (y<10){
                                y = "000"+y;
                        }
                        else if (y<100){
                                y = "00"+y;
                        }
                        else if (y<1000){
                                y = "0"+y;
                        }
                        return y;
                };
                var lokasi = config.location.id;
                var year = config.getDate()[2]+config.getDate()[3];
                var month = config.getDate()[5]+config.getDate()[6];
                return lokasi+year+month+id();
        }
    })
};
layout = {
    frame : new Layout({
        placeholder : ".frame"
    })
};
frame = {
    startUp : new Frame({
        placeholder : "#startUp",
        file : "layout/startUp.html",
        success : function(data){
            $("#startUp").html(data);
            frame.startUp.show();
            log.inject();
            fk.load();
            menu.main.inject();
            menu.main.hide();
            formz.login.inject();
            formz.login.load();
            formz.login.show();
        }
    }),
    kuitansiTransaksi : new Frame({
        placeholder : "#kuitansiTransaksi",
        file : "layout/kuitansiTransaksi.html",
        success : function(data){
            $("#kuitansiTransaksi").html(data);
        }
    })
};
log = new Log({
    placeholder : "#log"
});
menu = new Object();
menu.main = new Menu({
    placeholder : "#menu",
    id : "main",
    items : [
        {
            text : "Home",
            id : "main-menu-home",
            fungsi : function(){
                layout.frame.hide();
            }
        },
        {
            text : "Registrasi Donatur",
            id : "main-menu-registrasi-donatur",
            fungsi : function(){
                layout.frame.hide();
                formz.addDonatur.show();
                formz.addDonatur.resetValues();
            }
        },
        {
            text : "Transaksi Baru",
            id : "main-menu-transaksi",
            fungsi : function(){
                layout.frame.hide();
                formz.addTransaksi.show();
                formz.addTransaksi.resetValues();
            }
        },
        {
            text : "Database",
            id : "main-menu-database",
            fungsi : function(){
                layout.frame.hide();
                grid.donatur.show();
            }
        },
        {
            text : "Laporan",
            id : "main-menu-laporan",
            fungsi : function(){
                layout.frame.hide();
            }
        }
    ]
});
formz = new Object();
formz.login = new Formz ({
    placeholder : "#login",
    file : "layout/login.html",
    model : {
        username : null,
        password : null,
    },
    success : function(data){
        $("#login").html(data);
    },
    resetValues : function(){
        this.values = {
            username : null,
            password : null
        };
    }
});
formz.login.submit = function() {
    layout.frame.hide();
    menu.main.show();
};
formz.addDonatur = new Formz ({
    placeholder : "#addDonatur",
    file : "layout/addDonatur.html",
    create : config.server+"donatur/",
    success : function(data){
        $("#addDonatur").html(data);
        $("#formzAddDonatur :input[name='tanggalLahir']").kendoDatePicker({
            start: "century",
            format: "yyyy-MM-dd"
        });
        $("#formzAddDonatur :input[name='tempatLahir']").kendoAutoComplete({
            dataSource: fk.kota.view(),
            dataTextField: "namaKota"
        });
        $("#formzAddDonatur").kendoValidator();
        kendo.bind($("#formzAddDonatur"), model.donatur);
        formz.addDonatur.idAlamat = 1;
        formz.addDonatur.idKomunikasi = 1;
        model.donatur.alamat = new Array();
        model.donatur.komunikasi = new Array();
    },
    resetValues : function(){
        
    },
    simpan : function(){
        var x = $(this.placeholder).kendoValidator().data("kendoValidator");
        var hasil = x.validate();
        if (hasil===true){
            console.log("send!");
            var data = this.values;
            delete data.alamat;
            delete data.komunikasi;
            console.log("datanya = "+JSON.stringify(data));
        }
        else {
            console.log("gagal!");
        }
    }
});
formz.addDonatur.tambahAlamat = function(){
    $.ajax({
        url: "layout/addDonatur.tambahAlamat.html",
        cache: false,
        dataType: "html",
        success: function(data){
            $("#tambahAlamat").after(data);
            $(".alamat#0").attr({id:"a"+formz.addDonatur.idAlamat});
            var id = formz.addDonatur.idAlamat.toString();
            var index = formz.addDonatur.idAlamat-1;
            var alamatModel = {
                status : true,
                kategoris : fk.alamat_kategori.view(),
                kategori : null,
                tipes : fk.alamat_tipe.view(),
                tipe : null,
                alamat : null,
                provinsi : "null",
                provinsis : fk.provinsi.view(),
                kota : "null",
                kotas : fk.kota.view(),
                kecamatans : fk.kecamatan.view(),
                kecamatan : "null",
                kelurahans : fk.kelurahan.view(),
                kelurahan : "null",
                kodepos : null
            };
            var x = model.donatur.alamat[index] = kendo.observable(alamatModel);
            kendo.bind($(".alamat#a"+id), x);
            formz.addDonatur.idAlamat = formz.addDonatur.idAlamat+1;
            $("#formzAddDonatur").kendoValidator();
        }
    });
};
formz.addDonatur.hapusAlamat = function(){
    var id = event.currentTarget.parentElement.parentElement.attributes.id.value;
    var index = parseInt(id.replace("a",""))-1;
    $(".alamat#"+id+"").remove();
    model.donatur.alamat[index].set("status",false);
};
formz.addDonatur.tambahKomunikasi = function(){
     $.ajax({
        url: "layout/addDonatur.tambahKomunikasi.html",
        cache: false,
        dataType: "html",
        success: function(data){
            $("#tambahKomunikasi").after(data);
            $(".komunikasi#0").attr({id:"k"+formz.addDonatur.idKomunikasi});
            var id = formz.addDonatur.idKomunikasi.toString();
            var index = formz.addDonatur.idKomunikasi-1;
            var komunikasiModel = {
                status : true,
                jeniss : fk.komunikasi_jenis.view(),
                jenis : null,
                value : null
            };
            var x = model.donatur.komunikasi[index] = kendo.observable(komunikasiModel);
            kendo.bind($(".komunikasi#k"+id), x);
            formz.addDonatur.idKomunikasi=formz.addDonatur.idKomunikasi+1;
            $("#formzAddDonatur").kendoValidator();
        }
    });
};
formz.addDonatur.hapusKomunikasi = function(){
    var id = event.currentTarget.parentElement.parentElement.attributes.id.value;
    var index = parseInt(id.replace("k",""))-1;
    $(".komunikasi#"+id+"").remove();
    model.donatur.komunikasi[index].set("status",false);
};
formz.addTransaksi = new Formz ({
    placeholder : "#addTransaksi",
    file : "layout/addTransaksi.html",
    create : config.server+"transaksi/",
    success : function(data){
        $("#addTransaksi").html(data);
        $("#formzAddTransaksi :input[name='donatur']").kendoAutoComplete({
            dataSource: db.donatur.source.data(),
            dataTextField: "nama"
        });
        $("#formzAddTransaksi").kendoValidator();
        kendo.bind($("#formzAddTransaksi"), model.transaksi);
    },
    resetValues : function(){
    },
    simpan : function(){
        var x = $(this.placeholder).kendoValidator().data("kendoValidator");
        var hasil = x.validate();
        if (hasil===true){
            console.log("send!");
            layout.frame.hide();
            menu.main.hide();
            frame.kuitansiTransaksi.show();
        }
        else {
            console.log("gagal!");
        }
    }
});
$(document).ready(function(e) {
    config.startUp();
});